package cn.edu.scau.cmi.mazhuanglong.fxml;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import cn.edu.scau.cmi.mazhuanglong.abstractFactory.domainAbstractClass.ConsumerElectronicsDevice;
import cn.edu.scau.cmi.mazhuanglong.abstractFactory.factory.AbstractFactory;
import cn.edu.scau.cmi.mazhuanglong.abstractFactory.factory.AonongFactory;
import cn.edu.scau.cmi.mazhuanglong.abstractFactory.factory.TianbangFactory;
import cn.edu.scau.cmi.mazhuanglong.abstractFactory.factory.WensFactory;
import cn.edu.scau.cmi.mazhuanglong.client.SimpleFactoryMethodClient;
import cn.edu.scau.cmi.mazhuanglong.composite.consistent.ConsistentOrganization;
import cn.edu.scau.cmi.mazhuanglong.composite.consistent.ConsistentPerson;
import cn.edu.scau.cmi.mazhuanglong.composite.consistent.ConsistentTeam;
import cn.edu.scau.cmi.mazhuanglong.composite.saft.SafeOrganization;
import cn.edu.scau.cmi.mazhuanglong.composite.saft.SafePerson;
import cn.edu.scau.cmi.mazhuanglong.composite.saft.SafeTeam;
import cn.edu.scau.cmi.mazhuanglong.comprehension.Client.HibernateClient;
import cn.edu.scau.cmi.mazhuanglong.comprehension.Dao.StudentDAO;
import cn.edu.scau.cmi.mazhuanglong.comprehension.Dao.TeacherDAO;
import cn.edu.scau.cmi.mazhuanglong.comprehension.domain.Student;
import cn.edu.scau.cmi.mazhuanglong.comprehension.domain.Teacher;
import cn.edu.scau.cmi.mazhuanglong.factory.BeefFactory;
import cn.edu.scau.cmi.mazhuanglong.factory.ChickenFactory;
import cn.edu.scau.cmi.mazhuanglong.factory.MeatFactory;
import cn.edu.scau.cmi.mazhuanglong.factory.PorkFactory;
import cn.edu.scau.cmi.mazhuanglong.simpleFactoryMethod.domain.Meat;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;

public class lastoneController {
	@FXML
	private Button button1;
	@FXML
	private Button button2;
	@FXML
	private Button button3;
	@FXML
	private Button button4;
	@FXML
	private Button button5;
	@FXML
	private Button button6;
	@FXML
	private Button button7;
	@FXML
	private Button button8;
	@FXML
	private Button button9;
	@FXML
	private Button button10;

	private static void addNode(SafeTeam node, String childName, String teamName, Boolean isTeam) {
		if (node.getName().equals(teamName)) {
			SafeOrganization newNode = isTeam ? new SafeTeam(childName) : new SafePerson(childName);
			node.addChild(newNode);
			return;
		}
		for (SafeOrganization child : node.getChildren()) {
			if (child instanceof SafeTeam) {
				addNode((SafeTeam) child, childName, teamName, isTeam);
			}
		}
	}

	@FXML
	void Safe(ActionEvent event) throws IOException {
		SafeTeam root = new SafeTeam("Root");
		System.out.println("根节点：Root");
		String name;
		String team;
		Scanner str = new Scanner(System.in);
		while (true) {
			System.out.println("1、新增个人；2、新增团队；0、运行结束");
			int op = str.nextInt();
			if (op == 0)
				break;
			switch (op) {
			case 1: {
				System.out.println("输入个人名字");
				name = str.next();
				System.out.println("输入此人的团队");
				team = str.next();
				addNode(root, name, team, false);
				break;
			}
			case 2: {
				System.out.println("输入团队名字");
				name = str.next();
				System.out.println("输入团队的团队");
				team = str.next();
				addNode(root, name, team, true);
				break;
			}
			default:
				break;
			}
			Play(root, 0);
		}
	}

	private static void Play(SafeOrganization node, int depth) {
		for (int i = 0; i < depth; i++)
			System.out.print(" ");
		if (node instanceof SafePerson) {
			String name = ((SafePerson) node).getName();
			System.out.println(name + ":{");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("type:person");
		} else if (node instanceof SafeTeam) {
			String name = ((SafeTeam) node).getName();
			System.out.println(name + ":{");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("type:team");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("children:[");
			for (SafeOrganization child : ((SafeTeam) node).getChildren()) {
				Play(child, depth + name.length() + 12);
			}
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("]");
		}
		for (int i = 0; i < depth; i++)
			System.out.print(" ");
		System.out.println("}");
	}

	public static void addNode(ConsistentOrganization node, String childName, String teamName, Boolean isTeam) {
		String name = (node instanceof ConsistentTeam) ? ((ConsistentTeam) node).getName()
				: ((ConsistentPerson) node).getName();

		if (name.equals(teamName)) {
			ConsistentOrganization newNode = isTeam ? new ConsistentTeam(childName) : new ConsistentPerson(childName);
			node.addChild(newNode);
			return;
		}
		for (ConsistentOrganization child : node.getChildren()) {
			addNode(child, childName, teamName, isTeam);
		}
	}

	@FXML
	void Consistent(ActionEvent event) throws IOException {
		ConsistentOrganization root = new ConsistentTeam("Root");
		System.out.println("根节点：Root");
		String name;
		String team;
		Scanner str = new Scanner(System.in);
		while (true) {
			System.out.println("1、新增个人；2、新增团队；0、运行结束");
			int op = str.nextInt();
			if (op == 0)
				break;
			switch (op) {
			case 1: {
				System.out.println("输入个人名字");
				name = str.next();
				System.out.println("输入此人的团队");
				team = str.next();
				addNode(root, name, team, false);
				break;
			}
			case 2: {
				System.out.println("输入团队名字");
				name = str.next();
				System.out.println("输入团队的团队");
				team = str.next();
				addNode(root, name, team, true);
				break;
			}
			default:
				break;
			}
			Play(root, 0);
		}
	}

	private static void Play(ConsistentOrganization node, int depth) {
		for (int i = 0; i < depth; i++)
			System.out.print(" ");
		if (node instanceof ConsistentPerson) {
			String name = ((ConsistentPerson) node).getName();
			System.out.println(name + ":{");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("type:person");
		} else if (node instanceof ConsistentTeam) {
			String name = ((ConsistentTeam) node).getName();
			System.out.println(name + ":{");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("type:team");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("children:[");
			for (ConsistentOrganization child : node.getChildren()) {
				Play(child, depth + name.length() + 12);
			}
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("]");
		}
		for (int i = 0; i < depth; i++)
			System.out.print(" ");
		System.out.println("}");
	}

	@FXML
	void Adapter(ActionEvent event) throws IOException {

		FXMLLoader loader = new FXMLLoader();
		loader.setLocation(getClass().getResource("/cn/edu/scau/cmi/mazhuanglong/fxml/Adapter.fxml"));
		Parent root = (Parent) loader.load();
		Scene scene = new Scene(root);
		ChangeService.stage.setScene(scene);
		ChangeService.stage.show();
	}

	@FXML
	void Singleton(ActionEvent event) throws IOException {

		FXMLLoader loader = new FXMLLoader();
		loader.setLocation(getClass().getResource("/cn/edu/scau/cmi/mazhuanglong/fxml/Singleton.fxml"));
		Parent root = (Parent) loader.load();
		Scene scene = new Scene(root);
		ChangeService.stage.setScene(scene);
		ChangeService.stage.show();
	}

	@FXML
	void Factory(ActionEvent event) {
		System.out.println("Please select which kind of meat you want to eat: beef or pork or chicken???");
		Scanner scanner = new Scanner(System.in);
		String meatKind = scanner.next();
		MeatFactory meatFactory = null;
		switch (meatKind) {
		case "beef":
			meatFactory = new BeefFactory();
			break;
		case "pork":
			meatFactory = new PorkFactory();
			break;
		case "chicken":
			meatFactory = new ChickenFactory();
			break;
		default:
			System.out.println("sorry, your input is not correct");

		}
		if (meatFactory != null) {
			Meat meat = meatFactory.getMeat();
			meat.cook();
		}
	}

	@FXML
	void SimpleFactory(ActionEvent event) {
		new SimpleFactoryMethodClient();
	}

	@FXML
	void AbstractFactory(ActionEvent event) {
		System.out.println("Please select which brand you want to buy: Wens, Tianbang or Aonong???");
		Scanner scanner = new Scanner(System.in);
		String brandKind = scanner.next();
		AbstractFactory factory = null;
		switch (brandKind) {
		case "Wens":
			factory = new WensFactory();
			break;
		case "Tianbang":
			factory = new TianbangFactory();
			break;
		case "Aonong":
			factory = new AonongFactory();
			break;
		default:
			System.out.println("sorry, your input is not correct");

		}

//	（2）选择产品

		System.out.println("Please select which product you want to buy: pork, beef or chicken???");
//	Scanner scanner = new Scanner(System.in);  
		String productType = scanner.next();
		ConsumerElectronicsDevice electronics = null;
		switch (productType) {
		case "pork":
			electronics = factory.createPork();
			break;
		case "beef":
			electronics = factory.createBeef();
			break;
		case "chicken":
			electronics = factory.createChicken();
			break;
		default:
			System.out.println("sorry, your input is not correct");

		}
		if (electronics != null) {
			electronics.play();
		}
	}

	@FXML
	void Spirng(ActionEvent event) {
		Scanner input = new Scanner(System.in);
		String str = "1、添加学生" + "\n" + "2、修改学生" + "\n" + "3、删除学生" + "\n" + "4、添加教师" + "\n" + "5、修改教师" + "\n" + "6、删除教师"
				+ "\n" + "7、学生选择指导老师" + "\n" + "8、展示学生数据表";
		System.out.println(str);
		// int in = input.nextInt();
		while (true) {
			int in = input.nextInt();
			switch (in) {
			case 1: {
				System.out.println("输入姓名");
				String stuname = input.next();

				Student student = new Student();
				student.setName(stuname);

				StudentDAO instance = new StudentDAO();
				instance.save(student);
				break;

			}
			case 2: {
				System.out.println("输入你要修改的学生学号");
				long id = input.nextInt();

				System.out.println("输入你要修改后的学生姓名");
				String name = input.next();

				Student student = new Student();
				student.setId(id);
				student.setName(name);

				StudentDAO instance = new StudentDAO();
				instance.update(student);

				break;
			}
			case 3: {
				System.out.println("输入你要删除的学生学号");
				long id = input.nextInt();
				StudentDAO instance = new StudentDAO();

				instance.delete(instance.findById(id));

				break;
			}
			case 4: {
				System.out.println("输入姓名");
				String stuname = input.next();

				Teacher teacher = new Teacher();
				teacher.setName(stuname);

				TeacherDAO instance = new TeacherDAO();
				instance.save(teacher);
				break;
			}
			case 5: {
				System.out.println("输入你要修改的老师学号");
				long id = input.nextInt();

				System.out.println("输入你要修改后的老师姓名");
				String name = input.next();

				Teacher teacher = new Teacher();
				teacher.setId(id);
				teacher.setName(name);

				TeacherDAO instance = new TeacherDAO();
				instance.update(teacher);
				break;
			}
			case 6: {
				System.out.println("输入你要删除的教师学号");
				long id = input.nextInt();
				TeacherDAO instance = new TeacherDAO();

				instance.delete(instance.findById(id));

				break;
			}
			case 7: {
				System.out.println("选择学生id");
				long studentid = input.nextLong();
				StudentDAO stuinstance = new StudentDAO();
				stuinstance.findById(studentid);

				System.out.println("选择你的老师id");
				long teacherid = input.nextLong();
				TeacherDAO teainstance = new TeacherDAO();
				teainstance.findById(teacherid);

				Student student = new Student();
				student.setId(studentid);
				student.setName(stuinstance.findById(studentid).getName());
				student.setTeacher(teainstance.findById(teacherid));
				stuinstance.update(student);

				break;
			}
			case 8: {
				StudentDAO stu = new StudentDAO();
				List<Student> studentlist = stu.findAll();
				for (Student s : studentlist)

					System.out.println(s.getId() + "\t" + s.getName() + "\t");
				break;
			}
			default:
				System.out.print("运行结束");
				break;
			}
		}

	}
}
